package com.study.jdk.j8;

import java.util.Comparator;
import java.util.UUID;
import java.util.function.*;

/**
 * @author: jzhou
 * @date: 2024/12/1-14:36
 * @version: 1.0
 * @description: juc-study
 */
public class LambdaStudy {
    public static void main(String[] args) {
        //R apply(T t);函数型接口，一个参数，一个返回值
        Function<String, Integer> function = t -> t.length();
        System.out.println("函数型接口 Function<String, Integer> : " + function.apply("abcd"));

        //boolean test(T t);断定型接口，一个参数，返回boolean
        Predicate<String> predicate = t -> t.startsWith("b");
        System.out.println("断定型接口 Predicate<String> : " + predicate.test("bb"));
        System.out.println("断定型接口 Predicate<String> : " + predicate.test("aa"));

        // void accept(T t);消费型接口，一个参数，没有返回值
        Consumer<String> consumer = t -> {
            System.out.println("消费型接口 Consumer<String> : " + t);
        };
        consumer.accept("javaXXXX");

        //T get(); 供给型接口，无参数，有返回值
        Supplier<String> supplier = () -> {
            return UUID.randomUUID().toString();
        };
        System.out.println("供给型接口 Supplier<String> : " + supplier.get());

        Runnable r1 = () -> {
            // 获取线程名称
            System.out.println(Thread.currentThread().getName() + " : Hello from a lambda!");
        };
        // 创建一个线程并启动
        new Thread(r1).start();

        // ###########   其他函数式接口 ##########
        BinaryOperator<Integer> add = (a, b) -> a + b;
        System.out.println("BinaryOperator<Integer> : " + add.apply(1, 2));

        Comparator<String> comparator = (s1, s2) -> {
            if (s1.length() < s2.length()) return -1;
            else if (s1.length() > s2.length()) return 1;
            else return s1.compareTo(s2);
        };
        System.out.println("Comparator<String> : " + comparator.compare("aaa", "bbb"));
    }
}
